<?php
/**
* 短信控制器
*/
class Sms extends MY_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('User/User_model','user');
	}
	/**
	 * 发送验证码方法(微信+pc)
	 * @return [type] [description]
	 */
	function sms_send(){
		$post = $this->input->post();

		if(isset($post['phone'])){
			$_SESSION['phone'] = $post['phone'];


			$user = $this->user->getUserByPhone($post['phone']);
			if($user){
				$return = array(
					'code'=>0,
					'msg'=>'该号码已被注册',
					);

			}else{
				$code = rand(1000,9999);
		      // $code = 6666;
				$_SESSION['code'] = $code;
				$_SESSION['code_time'] = time() + 600;
		      // $res =
				$this->sms_post($code,$post['phone']);

		      // var_dump($res);die();
				$return = array(
					'code' => 1,
					'msg'  => '发送成功',
					);

			}
	  	// echo json_encode($return);
		}else{
			$return = array(
				'code'  => 0,
				'msg'   => '参数错误',
				);
		}
		echo json_encode($return);
	}
	//验证短信 
	public function verify(){
		$post = $this->input->post();
		
		if(isset($post['code'])&&isset($post['phone'])){
			if(isset($_SESSION['code_time']) && $_SESSION['code_time']>time()){
				// unset($_SESSION['code_time']);
				if($_SESSION['code'] == $post['code']){
		        	// unset($_SESSION['code']);
					if($_SESSION['phone']==$post['phone']){
				    	//用户第一步注册
						if(isset($_SESSION['userinfo']['openid'])){
							$post['openid'] = $_SESSION['userinfo']['openid'];
							$post['nickname'] = $_SESSION['userinfo']['nickname'];
							$post['country'] = $_SESSION['userinfo']['country'];
							$post['province'] = $_SESSION['userinfo']['province'];
							$post['city'] = $_SESSION['userinfo']['city'];
							$post['headimgurl'] = $_SESSION['userinfo']['headimgurl'];
							$post['sex'] = $_SESSION['userinfo']['sex'];
						}
				    	// print_r($_SESSION);exit();
						$res = $this->user->userInsert($post);
						if($res['code'] == 1){
							unset($_SESSION['code_time']);
							unset($_SESSION['code']);
				    		//更新session
							$_SESSION['userinfo'] = $this->user->getUser(array('id'=>$res['user_id']));
							$return = $res; 
						}else{
							$return = $res; 
						}
					}else{
						$return = array('code'=>0,'msg'=>'手机号码不匹配');
					}

				}else{
					$return = array('code'=>0,'msg'=>'验证码错误');
				}

			}else{
				$return = array('code' => 0,'msg' => '验证码不存在或已过期');
			}

		}else{
			$return = array('code'=>0,'msg'=>'注册信息缺失');
		}
		echo json_encode($return);
	}
    /**
     * 发送忘记密码短信验证码
     * @return [type] [description]
     */
    function send_forget_sms(){
    	$post = file_get_contents("php://input");

    	$post = json_decode($post,true);
    	if(isset($post['phone'])){
    		$this->load->model('Member_model','user');
    		$user = $this->user->getMemberByAccount($post['phone']);
    		if($user&&$user['is_complete']==1){
    			$_SESSION['forget_phone'] = $post['phone'];
    			$code = rand(1000,9999);

    			$_SESSION['forget_code'] = $code;
    			$_SESSION['forget_code_time'] = time() + 600;
    			$res = $this->sms_post($code,$post['phone']);
    			$return = array('code'=>1,'msg'=>'发送成功');
    		}else{
    			$return = array('code'=>0,'msg'=>'该账户不存在');
    		}
    	}else{
    		$return = array('code'=>0,'msg'=>'手机号码错误');
    	}
    	echo json_encode($return);
    }
    /**
     * 短信发送接口
     * @return [type] [description]
     */
    function sms_post($code,$phone){
    		// $this->load->model('Sms_model','sms');
    		// $smsinfo = $this->sms->getSmsOne();
    		// if(empty($smsinfo))
    		// {
    		// 	return json_encode(array('code' =>0,'未找到短信配置'));
    		// }
    	$smsinfo = array(
    		'urls'   => 'https://sms.yunpian.com/v2/sms/tpl_single_send.json',
    		'apikey' => '42f01458ce3dc39a3e2f5a6edd96999b',
    		'tpl_id' => '1882028',
    		);
    	$url = $smsinfo['urls']; 
    	$apikey = $smsinfo['apikey']; 
    	$post_data = array(
    		'tpl_id'=>$smsinfo['tpl_id'],
    		'tpl_value'=>('#code#').'='.urlencode($code),
    		'apikey'=>$apikey,
    		'mobile'=>$phone,
    		);
    	$post_data = http_build_query($post_data);
			//请求发短信验证码
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_URL,$url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    	$result = curl_exec($ch);
    	curl_close($ch);
    	$result = json_decode($result,true);
    	return $result;
    }
    /**
     * app短信验证
     * @DateTime 2017-07-21
     * @author zyl
     * @return   [type]     [description]
     */
    function app_sms_send(){
    	$post = file_get_contents("php://input");
    	$post = json_decode($post,true);
        // ksort($post); //键名升序
    	$str = '';
    	foreach($post as $key=>$val){
    		if(!is_array($val) && $key!='sign'){
    			$str.=$key.'='.$val.'&';
    		}
    	}
        // print_r($str);die();
    	$sign = md5($str.'key='.APP_KEY);
    	if($post['sign']!=$sign){
    		$return = array('code'=>0,'msg'=>'签名验证失败','smscode'=>'');
    		echo json_encode($return);return;
    	}
		// print_r($sign);die();  
    	$user = $this->user->getUserByPhone($post['phonenum']);
    	if($user){
    		$return = array(
    			'code'    => 2,
    			'msg'     => '该号码已被注册',
    			'smscode' => ''
    			);

    	}else{
    		$code = rand(1000,9999);
	      // $code = 6666;
	   //    $_SESSION['code'] = $code;
		  // $_SESSION['code_time'] = time() + 600;
    		$this->sms_post($code,$post['phonenum']);

	      // var_dump($res);die();
    		$return = array(
    			'code'    => 1,
    			'msg'     => '发送成功',
    			'smscode' => $code,
    			);

    	}
    	echo json_encode($return);
    }
    /**
     * app忘记密码
     * @DateTime 2017-07-21
     * @author zyl
     * @return   [type]     [description]
     */
    function app_forget_sms(){
    	$post = file_get_contents("php://input");
    	$post = json_decode($post,true);
        // ksort($post); //键名升序
    	$str = '';
    	foreach($post as $key=>$val){
    		if(!is_array($val) && $key!='sign'){
    			$str.=$key.'='.$val.'&';
    		}
    	}
        // print_r($str);die();
    	$sign = md5($str.'key='.APP_KEY);
    	if($post['sign']!=$sign){
    		$return = array('code'=>0,'msg'=>'签名验证失败','smscode'=>'');
    		echo json_encode($return);return;
    	}
    	$user = $this->user->getUserByPhone($post['phonenum']);
    	if(!$user){
    		$return = array(
    			'code'    => 2,
    			'msg'     => '号码不存在',
    			'smscode' => ''
    			);

    	}else{
    		$code = rand(1000,9999);
	   //    $_SESSION['code'] = $code;
		  // $_SESSION['code_time'] = time() + 600;
	      // $code = 6666;
    		$this->sms_post($code,$post['phonenum']);

	      // var_dump($res);die();
    		$return = array(
    			'code'    => 1,
    			'msg'     => '发送成功',
    			'smscode' => $code,
    			);

    	}
    	echo json_encode($return);
    }
    /**
     * 余额支付短信验证
     * @DateTime 2017-08-08
     * @author zyl
     * @return   [type]     [description]
     */
    function app_pay_sms(){
    	$post = file_get_contents("php://input");
    	$post = json_decode($post,true);
        // ksort($post); //键名升序
    	$str = '';
    	foreach($post as $key=>$val){
    		if(!is_array($val) && $key!='sign'){
    			$str.=$key.'='.$val.'&';
    		}
    	}
        // print_r($str);die();
    	$sign = md5($str.'key='.APP_KEY);
    	if($post['sign']!=$sign){
    		$return = array('code'=>0,'msg'=>'签名验证失败','smscode'=>'');
    		echo json_encode($return);return;
    	}
    	$user = $this->user->getUserByPhone($post['phonenum']);
    	if (!$user) {
    		$return = array(
    			'code'    => 2,
    			'msg'     => '号码不存在',
    			'smscode' => ''
    			);

    	} else {
    		$code = rand(1000,9999);
    		$_SESSION['code'] = $code;
    		$_SESSION['code_time'] = time() + 600;
    		$_SESSION['phone'] = $post['phonenum'];
	      // $code = 6666;
    		$this->sms_post($code,$post['phonenum']);

	      // var_dump($res);die();
    		$return = array(
    			'code'    => 1,
    			'msg'     => '发送成功',
    			'smscode' => $code,
    			);

    	}
    	echo json_encode($return);
    }
    /**
     * 销售短信验证
     * @DateTime 2017-08-15
     * @author zyl
     * @return   [type]     [description]
     */
    function seller_sms_send(){
    	$post = $this->input->post();

    	if(isset($post['phone'])){
    		$_SESSION['phone'] = $post['phone'];

    		$this->load->model('MerchantStaff_model','staff');
    		$where['phone'] = $post['phone'];
    		$staff = $this->staff->getStaffDetail($where);
		  // $user = $this->user->getUserByPhone($where);
    		if(!$staff){
    			$return = array(
    				'code'=>0,
    				'msg'=>'无该号码信息',
    				);

    		}else{
    			$code = rand(1000,9999);
		      // $code = 6666;
    			$_SESSION['code'] = $code;
    			$_SESSION['code_time'] = time() + 600;
		      // $res =
    			$this->sms_post($code,$post['phone']);

		      // var_dump($res);die();
    			$return = array(
    				'code' => 1,
    				'msg'  => '发送成功',
    				);

    		}
	  	// echo json_encode($return);
    	}else{
    		$return = array(
    			'code'  => 0,
    			'msg'   => '参数错误',
    			);
    	}
    	echo json_encode($return);
    }
	/**
     * 银行人员短信验证
     * @DateTime 2017-08-17
     * @author xyl
     * @return   [type]     [description]
     */
	function banker_sms_send(){
		$post = $this->input->post();

		if(isset($post['phone'])){
			$_SESSION['phone'] = $post['phone'];

			$this->load->model('Banker_model','banker');
			$where['phone'] = $post['phone'];
			$staff = $this->banker->getBankerInfo($where);
		  // $user = $this->user->getUserByPhone($where);
			if(!$staff){
				$return = array(
					'code'=>0,
					'msg'=>'无该号码信息',
					);
			}else{
				$code = rand(1000,9999);
		      // $code = 6666;
				$_SESSION['code'] = $code;
				$_SESSION['code_time'] = time() + 600;
		      // $res =
				$this->sms_post($code,$post['phone']);

		      // var_dump($res);die();
				$return = array(
					'code' => 1,
					'msg'  => '发送成功',
					);
			}
	  	// echo json_encode($return);
		}else{
			$return = array(
				'code'  => 0,
				'msg'   => '参数错误',
				);
		}
		echo json_encode($return);
	}
}